package com.itboye.ihomebank;

import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.ihomebank.adapter.KeyAdapter;
import com.itboye.ihomebank.base.BaseFragment;
import com.itboye.ihomebank.base.MyApplcation;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.custom.XListView;
import com.itboye.ihomebank.key.ActivityGuanLiKey;
import com.itboye.ihomebank.key.ActivitySheBei;
import com.itboye.ihomebank.presenter.KeyPresenter;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.SPUtils;

/**
 * 钥匙
 * 
 */
public class KeyFragment extends BaseFragment implements OnClickListener,
		Observer {
	ImageView close_icon,img_nodata;
	TextView add_shap_title_tv;
	XListView xlistview;
	TextView mykey_sousuo;// 搜索
	private KeyAdapter adapter;
	
	String uid;
	UserPresenter userPresenter;
	KeyPresenter keyPresenter;
	
	@Override
	public int initView() {
		return R.layout.activity_my_key;
	}

	@Override
	public void onMyActivityCreated() {
		close_icon.setVisibility(View.GONE);
		add_shap_title_tv.setText("我的智能锁");
		uid = SPUtils.get(getActivity(), null, SPContants.USER_ID, "") + "";
		String sciner = SPUtils.get(getActivity(), null, SPContants.ScienerUsername, "") + "";//科技侠账号
		
		if(sciner==""){
			keyPresenter=new KeyPresenter(this);
			keyPresenter.getRegister(uid);
		}
		

	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

	}

	public void onClick(View v) {
		Intent intent = null;
		switch (v.getId()) {
		case R.id.mykey_sousuo:// 搜索
			intent = new Intent(getActivity(), ActivityGuanLiKey.class);
			startActivity(intent);
			break;
		case R.id.add_shap_title_tv:// 标题 暂时添加跳转
			intent = new Intent(getActivity(), ActivitySheBei.class);
			startActivity(intent);
			break;

		default:
			break;
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == KeyPresenter.register_success) {
				SPUtils.put(MyApplcation.ctx, null,
						SPContants.ScienerUsername,
						entity.getData().toString());
			}
			if (entity.getEventType() == KeyPresenter.register_fail) {
			}
		}
	}
}
